﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Members;
namespace Invoicing.Command.Members
{
    public class CustomerCategoryCommand
    {
        public static CustomerCategoryModel CreateNew(CustomerCategoryModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var dCategory = Convertor.ConvertFrom<CustomerCategory, CustomerCategoryModel>(model);
                    db.DataContext.CustomerCategories.Add(dCategory);
                    db.DataContext.SaveChanges();
                    model.ID = dCategory.ID;
                    return model;
                }
            }
            return null;
        }

        public static void Update(CustomerCategoryModel model)
        {
            if (model.ID > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCategory = db.DataContext.CustomerCategories.FirstOrDefault(p => p.ID == model.ID);
                    if (dCategory == null) throw new ApplicationException("不存在的分类编号");
                    SaveCheck(model, db);
                    Convertor.UpdateValue(model, dCategory);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void Delete(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<CustomerCategory>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }

        private static void SaveCheck(CustomerCategoryModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.CategoryName))
                throw new ApplicationException("请提供分类名称");
            if (db != null)
            {
                if (db.DataContext.CustomerCategories.Any(p => p.CategoryName == model.CategoryName && p.ID != model.ID))
                    throw new ApplicationException("分类名称重复，请另外输入一个。");
            }
        }

        public static CustomerCategoryModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCategory = db.DataContext.CustomerCategories.FirstOrDefault(p => p.ID == id);
                    if (dCategory != null)
                    {
                        var model = Convertor.ConvertTo<CustomerCategoryModel, CustomerCategory>(dCategory);
                        return model;
                    }
                }
            }
            return null;
        }

        public static CustomerCategoryCollection GetCategories(CustomerCategoryQuery sea)
        {
            var result = new CustomerCategoryCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<CustomerCategory> q = db.DataContext.CustomerCategories;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.CategoryName.Contains(sea.Key));
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<CustomerCategoryModel, CustomerCategory, CustomerCategoryCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }
    }
}
